<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仪表名称" prop="ybName">
        <el-input
          v-model="queryParams.ybName"
          placeholder="请输入仪表名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="仪表类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item.ybTpId"
            :label="item.ybTpName"
            :value="item.ybTpId"
            @keyup.enter.native="handleQuery"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="小区名称" prop="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择">
          <el-option
            v-for="item in communitys"
            :key="item.communityId"
            :label="item.communityName"
            :value="item.communityId"
            @keyup.enter.native="handleQuery"
          >
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yibiaoManagement:money:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yibiaoManagement:money:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yibiaoManagement:money:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yibiaoManagement:money:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="ybMoyId" />
      <el-table-column label="所在小区" align="center" prop="wyCommunity.communityName" />
      <el-table-column label="房间号" align="center" prop="wyHome.homeNumber" />
      <el-table-column label="收费类型" align="center" prop="wyYbType.ybTpName" />
      <el-table-column label="起度" align="center" prop="ybMoyBeginElectricity" />
      <el-table-column label="止度" align="center" prop="ybMoyEndElectricity" />
      <el-table-column label="开始收费的时间" align="center" prop="ybMoyBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ybMoyBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截至收费的时间" align="center" prop="ybMoyEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ybMoyEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
	  
	  <el-table-column label="备注" align="center" prop="ybMoyRemark" width="180">
	  	<template slot-scope="scope">
	  		{{ delTag(scope.row.ybMoyRemark) }}
	  	</template>
	  </el-table-column>
	  
      <!-- <el-table-column label="备注" align="center" prop="ybMoyRemark" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="ybMoyCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ybMoyCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="需缴费" align="center" prop="ybMoyMoney" />
      <!-- <el-table-column label="是否缴费" align="center" prop="ybMoyStatus" /> -->
      <el-table-column label="是否缴费" align="center" prop="ybMoyStatus">
        <template v-slot="scope">
          <span>{{ scope.row.ybStatus === 0 ? "未缴费" : "已缴费" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateBiao(scope.row)"
            v-hasPermi="['yibiaoManagement:money:edit']"
          >换表</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yibiaoManagement:money:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪表-----周期收费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仪表id" prop="ybId">
          <el-input v-model="form.ybId" placeholder="请输入仪表id" />
        </el-form-item>
        <el-form-item label="起度" prop="ybMoyBeginElectricity">
          <el-input v-model="form.ybMoyBeginElectricity" placeholder="请输入起度" />
        </el-form-item>
        <el-form-item label="止度" prop="ybMoyEndElectricity">
          <el-input v-model="form.ybMoyEndElectricity" placeholder="请输入止度" />
        </el-form-item>
        <el-form-item label="备注" prop="ybMoyRemark">
          <el-input v-model="form.ybMoyRemark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建时间" prop="ybMoyCreateTime">
          <el-date-picker clearable
            v-model="form.ybMoyCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="需缴费" prop="ybMoyMoney">
          <el-input v-model="form.ybMoyMoney" placeholder="请输入需缴费" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {updateMoneyBiao, listMoney, getMoney, delMoney, addMoney, updateMoney } from "@/api/yibiaoManagement/money";
import { listTypes, listCommunity,} from "@/api/yibiaoManagement/yibiao";
export default {
  name: "Money",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪表-----周期收费表格数据
      moneyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ybName: null,
        communityId: null,
        typeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      communitys:[],
      types:[],
    };
  },
  created() {
    this.getList();
    this.getCommunityList();
    this.getTypes();
  },
  methods: {
	  //去除富文本的标签
	  delTag(str){
	  	return str.replace(/<[^>]+>/g,"");
	  },
      /** 查询仪表类型列表 */
      getTypes() {
      this.loading = true;
      listTypes().then((response) => {
        this.types=response.rows;
      });
    },
     /** 查询所有小区信息 */
     getCommunityList() {
      this.loading = true;
      listCommunity().then((response) => {
        this.communitys = response.rows;
        this.loading = false;
      });
    },
    /** 查询仪表-----周期收费列表 */
    getList() {
      this.loading = true;
      listMoney(this.queryParams).then(response => {
        this.moneyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ybMoyId: null,
        ybId: null,
        ybMoyBeginElectricity: null,
        ybMoyEndElectricity: null,
        ybMoyBeginTime: null,
        ybMoyEndTime: null,
        ybMoyRemark: null,
        ybMoyCreateTime: null,
        del: null,
        ybMoyMoney: null,
        ybMoyStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ybMoyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仪表-----周期收费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ybMoyId = row.ybMoyId || this.ids
      getMoney(ybMoyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仪表-----周期收费";
      });
    },

    /** 换表 */
    handleUpdateBiao(row) {
      this.reset();
      // const ybMoyId = row.ybMoyId || this.ids
      updateMoneyBiao(row).then(response => {
        
      });
      this.getList()
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ybMoyId != null) {
            updateMoney(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMoney(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ybMoyIds = row.ybMoyId || this.ids;
      this.$modal.confirm('是否确认删除仪表-----周期收费编号为"' + ybMoyIds + '"的数据项？').then(function() {
        return delMoney(ybMoyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yibiaoManagement/money/export', {
        ...this.queryParams
      }, `money_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
